package com.sangeng.service.impl;

import com.sangeng.domain.ResponseResult;
import com.sangeng.enums.AppHttpCodeEnum;
import com.sangeng.handler.exception.SystemException;
import com.sangeng.service.UploadService;
import com.sangeng.utils.AliOssUtil;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * ClassName: OssUploadService
 * Package: com.sangeng.service.impl
 * Description:
 *
 * @Author hjh
 * @Create 2024/8/17 17:55
 * @Version 1.0
 */
@Service
@Data
@ConfigurationProperties(prefix = "oss")
public class OssUploadService implements UploadService {
    @Autowired
    private AliOssUtil aliOssUtil;
    @Override
    public ResponseResult uploadImg(MultipartFile img) {
        String url = null;
        try {
        //判断文件类型
        //获取原始文件名
        String originalFilename = img.getOriginalFilename();
        //对原始文件名进行判断
        if(!originalFilename.endsWith(".png")){
            throw new SystemException(AppHttpCodeEnum.FILE_TYPE_ERROR);
        }

        //如果判断通过上传文件到OSS
        url = aliOssUtil.upload(img.getBytes(),originalFilename);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return ResponseResult.okResult(url);
    }
}
